<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Conf_model extends CI_Model  {
	
	function _construct() {
      parent::_construct();
	}
	
	public function get_users_list($arr) {
		$str = implode(',', $arr);
		$query = $this->db->query("select 
									    p.perId,
									    concat(p.secondName,
									            ' ',
									            p.firstName,
									            ' ',
									            p.middleName) as FIO,
									    pa.mptext
									from
									    person p
									        join
									    personadditionalcolumns pa on (pa.perId = p.perId and pa.tcId = 3)
									where
									    p.perId in ($str)
									order by FIO");
		if ($query->num_rows() > 0) {
			$row = $query->result_array();
			return $row;
		}
	}
	
	public function get_xml_configuration () {
		$tmp = new xmlSettings();
		if ($tmp->check_settings()) {
			switch ($tmp->get_saId()) {
				case '6521B186-3E2B-441d-A09A-D0B5F47534B7':
					$resp['confText'] = 'Пробне тестування';
					$subj = $tmp->get_subjects();
					$subjList1 = $tmp->get_subj_list('-1');
					$subjList2 = $tmp->get_subj_list('-2');
					$str1 = explode(':', $subj[0]);
					$str2 = explode(':', $subj[1]);
					$resp['days'][] = $str1[1].';'.(implode(',', $subjList1));
					$resp['days'][] = $str2[1].';'.(implode(',', $subjList2));
				break;
				case '8006FC48-691A-4438-8176-AAAE9BCC90DB':
					$resp['confText'] = 'Основне тестування';
					$subj = $tmp->get_subjects();
					foreach ($subj as $tmp) {
						$str = explode(':', $tmp);
						$str1 = explode(';', $str[0]);
						$resp['days'][] = $str[1].';'.$this->get_subj_name($str1[0]).','.($str1[1] == 1?'I сессія':'II сессія');
					}
				break;
			}
		} else {
			$resp['confText'] = 'Не знайдено жодної попередньої конфігурації!';
			$resp['confError'] = 13;
		}
		return $resp;
	}
	
	private function get_subj_name($subjId) {
		$query = $this->db->query("select subjName from subjects where subjId=$subjId");
		if ($query->num_rows() > 0) {
			$row = $query->row();
			return $row->subjName;
		}
	}
	
	public function get_subjects_list() {
		$selectStr = '(1,2,3,4,5,6,7,8,16,18)';
		$query = $this->db->query("select subjId, subjName from subjects where subjId in $selectStr");
		if ($query->num_rows() > 0) {
			$row = $query->result_array();
			return $row;
		}
	}
	
	public function save_prob_conf($days, $saId) {
		$str = '<?xml version="1.0"?>
					<title type="testing">Zvit settings
					<settings>';
		$str .= "<saId>$saId</saId>";
		$str .= '<subjects>';
		$i = 1;
		foreach ($days as $key => $value) {
			$str .= "<day$i>-$i;1:$key</day$i>";
			$n = count($value);
			for ($j=1; $j <= $n ; $j++) { 
				$tmp[$i][] = $value[$j-1];
			}
			$i++;
		}
		$str .= '</subjects><probSubjListDay1>';
		foreach ($tmp[1] as $key => $value) {
			$name = $this->get_subj_name($value);
			$str .= "<subj$key>$name;$value</subj$key>";
		}
		$str .= '</probSubjListDay1>';
		$str .= '<probSubjListDay2>';
		foreach ($tmp[2] as $key => $value) {
			$name = $this->get_subj_name($value);
			$str .= "<subj$key>$name;$value</subj$key>";
		}
		$str .= '</probSubjListDay2>';
		$str .= '</settings>
					</title>';
		$xml = new xmlSettings();
		$fp = fopen($xml->get_file_name(), 'w+');
		fwrite($fp, $str);
		fclose($fp);
	}
	
	public function save_osn_conf($data, $saId) {
		$str = '<?xml version="1.0"?>
					<title type="testing">Zvit settings
					<settings>';
		$str .= "<saId>$saId</saId>";
		$str .= '<subjects>';
		$i = 1;
		foreach ($data as $tmp) {
			$str .= "<day$i>$tmp</day$i>";
			$i++;
		}
		$str .= '</subjects></settings>
					</title>';
		$xml = new xmlSettings();
		$fp = fopen($xml->get_file_name(), 'w+');
		fwrite($fp, $str);
		fclose($fp);
	}
}